package com.wlyy.his.controller.registration;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wlyy.his.bean.register.dto.ScheduleSourceNumUpdateDto;
import com.wlyy.his.bean.register.qo.GuaHaoSourceQo;
import com.wlyy.his.bean.register.vo.GuaHaoSourceVo;
import com.wlyy.his.service.registration.IHisDoctorScheduleService;
import com.wlyy.his.entity.registration.HisDoctorSchedule;
import com.wlyy.his.common.api.CommonResult;
import com.wlyy.his.ze.api.ZeGuoNeiApiService;
import com.wlyy.his.ze.bean.base.BASEINFO;
import com.wlyy.his.ze.bean.request.GUAHAOHYXX_IN_request;
import com.wlyy.his.ze.bean.response.GUAHAOHYXX_OUT;
import com.wlyy.his.ze.hepler.BeanCoverUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author 10097
 * @since 2021-09-01
 */
@RestController
@RequestMapping("/schedule")
public class HisDoctorScheduleController {
    @Resource
    public IHisDoctorScheduleService hisDoctorScheduleService;


    /**
     * 通过科室查询排班信息
     *
     * @param hisCode  分院编码
     * @param deptCode 科室编码
     * @return 结果
     */
    @GetMapping("dept/find")
    public CommonResult<List<HisDoctorSchedule>> findByDept(@RequestParam String hisCode, @RequestParam String deptCode) {
        List<HisDoctorSchedule> schedules = hisDoctorScheduleService.list(Wrappers.lambdaQuery(HisDoctorSchedule.class).eq(HisDoctorSchedule::getHisCode, hisCode).eq(HisDoctorSchedule::getDeptCode, deptCode));
        return CommonResult.success(schedules);
    }

    /**
     * 根据医生查询号源
     *
     * @param hisCode    院区编码
     * @param doctorCode 医生编码
     * @return 结果
     */
    @GetMapping("doctor/find")
    public CommonResult<List<HisDoctorSchedule>> findByDoctor(@RequestParam String hisCode, @RequestParam String doctorCode) {
        LambdaQueryWrapper<HisDoctorSchedule> queryWrapper = Wrappers.lambdaQuery(HisDoctorSchedule.class)
                .eq(HisDoctorSchedule::getHisCode, hisCode)
                .eq(HisDoctorSchedule::getDoctorCode, doctorCode);
        List<HisDoctorSchedule> schedules = hisDoctorScheduleService.list(queryWrapper);
        return CommonResult.success(schedules);
    }

    /**
     * 号源信息查询
     *
     * @param qo    入参
     * @return 结果
     */
    @PostMapping("haoyuan")
    public CommonResult<List<GuaHaoSourceVo>> findHaoYuan(@RequestBody @Validated GuaHaoSourceQo qo) {
        //可以考虑添加一个较短的缓存，但目前直接从医院接口获取
        GUAHAOHYXX_IN_request request = JSON.parseObject(JSON.toJSONStringWithDateFormat(qo, "yyyy-MM-dd"), GUAHAOHYXX_IN_request.class);
        BASEINFO baseinfo = new BASEINFO();
        baseinfo.setFENYUANDM(qo.getHisCode());
        request.setBASEINFO(baseinfo);

        GUAHAOHYXX_OUT out = ZeGuoNeiApiService.guahaoSource00533(request);
        if (CollUtil.isNotEmpty(out.getHAOYUANXX())) {
            //更新对应排班的剩余数量 最好走异步
            ScheduleSourceNumUpdateDto updateDto = new ScheduleSourceNumUpdateDto();
            updateDto.setHisCode(qo.getHisCode());
            updateDto.setDeptCode(qo.getDeptCode());
            updateDto.setDoctorCode(StrUtil.isEmpty(qo.getDoctorCode()) ? "*" : qo.getDoctorCode());
            updateDto.setScheduleShift(qo.getScheduleShift());
            if ("1".equals(qo.getScheduleShift())) {
                updateDto.setSourceAmLeft(out.getHAOYUANXX().size() + "");
            } else {
                updateDto.setSourcePmLeft(out.getHAOYUANXX().size() + "");
            }
            hisDoctorScheduleService.updateSourceNum(updateDto);
           return CommonResult.success(BeanCoverUtil.coverBitch(out.getHAOYUANXX(), new GuaHaoSourceVo()));
        }
        return CommonResult.success(Collections.emptyList());
    }
}

